using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using ARR.Entities.Gateways;

namespace ARR.DataModel.Repositories
{
	public static partial class TempArticlesRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class TempArticlesDbo : DatabaseObject
        {
            public TempArticlesDbo()
            {
                Name = "TempArticles";
                GeneratedClassName = "TempArticles";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "RangeReviewId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "RangeReviewId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "smallint", IsIdentity = false, IsNullable = true, Length = 2, Precision = 5, Scale = 0, GeneratedPropertyName = "CategoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Code", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 20, Precision = 0, Scale = 0, GeneratedPropertyName = "Code" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Name", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Name" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "EAN", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "EAN" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "UOM", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "UOM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Brand", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Brand" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Manufacturer", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 255, Precision = 0, Scale = 0, GeneratedPropertyName = "Manufacturer" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "NotFoundCategory", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "NotFoundCategory" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Size", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Size" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Width", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Width" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Height", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Height" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Facings", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "Facings" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute1", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute1" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute2", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute2" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute3", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute3" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute4", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute4" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Attribute5", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Attribute5" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TaxRate", ColumnType = "smallmoney", IsIdentity = false, IsNullable = true, Length = 4, Precision = 10, Scale = 4, GeneratedPropertyName = "TaxRate" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Type", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Type" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "OrderUOM", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "OrderUOM" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "OrderEAN", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "OrderEAN" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Pack", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Pack" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Vendor", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 100, Precision = 0, Scale = 0, GeneratedPropertyName = "Vendor" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Price", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Price" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Cost", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "Cost" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "NetPrice", ColumnType = "money", IsIdentity = false, IsNullable = true, Length = 8, Precision = 19, Scale = 4, GeneratedPropertyName = "NetPrice" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CreatedDate", ColumnType = "date", IsIdentity = false, IsNullable = true, Length = 3, Precision = 10, Scale = 0, GeneratedPropertyName = "CreatedDate" });
              
            }
        }
		
		public static List<TempArticleRow> Select(Expression<Func<TempArticleRow, bool>> expression)
        {
            List<TempArticleRow> rows = new List<TempArticleRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<TempArticleRow>(new TempArticlesDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<TempArticleRow> Select()
		{
			List<TempArticleRow> rows = new List<TempArticleRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<TempArticleRow>(new TempArticlesDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<TempArticleRow> PagedSelect(Expression<Func<TempArticleRow, bool>> filterExpression, Expression<Func<TempArticleRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<TempArticleRow> rows = new List<TempArticleRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<TempArticleRow>(new TempArticlesDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<TempArticleRow> TempArticleList)
        {
            TempArticleRow row;

            while (reader.Read())
            {
                row = new TempArticleRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					RangeReviewId = Util.CastDbValue<int>(reader["RangeReviewId"]),
					CategoryId = Util.CastDbValue<int?>(reader["CategoryId"]),
					Code = Util.CastDbValue<string>(reader["Code"]),
					Name = Util.CastDbValue<string>(reader["Name"]),
					EAN = Util.CastDbValue<string>(reader["EAN"]),
					UOM = Util.CastDbValue<string>(reader["UOM"]),
					Brand = Util.CastDbValue<string>(reader["Brand"]),
					Manufacturer = Util.CastDbValue<string>(reader["Manufacturer"]),
					NotFoundCategory = Util.CastDbValue<string>(reader["NotFoundCategory"]),
					Size = Util.CastDbValue<decimal?>(reader["Size"]),
					Width = Util.CastDbValue<decimal?>(reader["Width"]),
					Height = Util.CastDbValue<decimal?>(reader["Height"]),
					Facings = Util.CastDbValue<decimal?>(reader["Facings"]),
					Attribute1 = Util.CastDbValue<string>(reader["Attribute1"]),
					Attribute2 = Util.CastDbValue<string>(reader["Attribute2"]),
					Attribute3 = Util.CastDbValue<string>(reader["Attribute3"]),
					Attribute4 = Util.CastDbValue<string>(reader["Attribute4"]),
					Attribute5 = Util.CastDbValue<string>(reader["Attribute5"]),
					TaxRate = Util.CastDbValue<decimal?>(reader["TaxRate"]),
					Type = Util.CastDbValue<string>(reader["Type"]),
					OrderUOM = Util.CastDbValue<string>(reader["OrderUOM"]),
					OrderEAN = Util.CastDbValue<string>(reader["OrderEAN"]),
					Pack = Util.CastDbValue<string>(reader["Pack"]),
					Vendor = Util.CastDbValue<string>(reader["Vendor"]),
					Price = Util.CastDbValue<decimal?>(reader["Price"]),
					Cost = Util.CastDbValue<decimal?>(reader["Cost"]),
					NetPrice = Util.CastDbValue<decimal?>(reader["NetPrice"]),
					CreatedDate = Util.CastDbValue<DateTime?>(reader["CreatedDate"])
                };
                                
                TempArticleList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(TempArticleRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<TempArticleRow>(new TempArticlesDbo(), row);
            }
        }

		public static void Update(TempArticleRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<TempArticleRow>(new TempArticlesDbo(), row);
            }
        }

		public static void Delete(Expression<Func<TempArticleRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<TempArticleRow>(new TempArticlesDbo(), expression);
            }
        }

	}
}
	
